package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * First page that the user sees and first page on UINavigation controller stack.
 * Will give a map around the users current location and a blue dot of their
 * exact location determined by the MKMapView class. User can confirm location with 
 * a button on the bottom of the map.
 * 
 */
import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.CLLocation;
import org.xmlvm.iphone.CLLocationCoordinate2D;
import org.xmlvm.iphone.CLLocationManager;
import org.xmlvm.iphone.CLLocationManagerDelegate;
import org.xmlvm.iphone.MKCoordinateRegion;
import org.xmlvm.iphone.MKCoordinateSpan;
import org.xmlvm.iphone.MKMapView;
import org.xmlvm.iphone.UIButton;
import org.xmlvm.iphone.UIButtonType;
import org.xmlvm.iphone.UIControl;
import org.xmlvm.iphone.UIControlDelegate;
import org.xmlvm.iphone.UIControlEvent;
import org.xmlvm.iphone.UIControlState;
import org.xmlvm.iphone.UIScreen;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewController;

class MapPage extends UIViewController {
	/* 
	 * Hard coded longitude and latitude of current location 
	 * but will be overwritten when MKMapView is triggered.
	 */
    private double latitude = 37.767967;
    private double longitude = -122.426147;
    
    public MapPage() {
        this.setTitle("Location");
    }   
    
    public void setLat(double lat){
    	latitude = lat;
    }
    
    public void setLon(double lon){
    	longitude = lon;
    }
    
    @Override
    public void loadView() {
       	super.loadView();
		UIView root = getView();
        CGRect rect = UIScreen.mainScreen().getApplicationFrame();
        rect.origin.x = rect.origin.y = 0;
        MKMapView mapView = new MKMapView(rect);
        mapView.setShowsUserLocation(true);
        MKCoordinateRegion region;
        final MKCoordinateSpan span = new MKCoordinateSpan(0.01, 0.01);
        
        CLLocationManager manager = new CLLocationManager();     
        manager.setDelegate(new CLLocationManagerDelegate() {    	 
            @Override
            public void didUpdateToLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation) {
                /**
                 * Whenever the location updates, this callback will be called
                 * by iOS. Retrieve the current longitude and latitude and
                 * display them in the appropriate labels.
                 */        	
            	setLat(newLocation.getCoordinate().latitude);
            	setLon(newLocation.getCoordinate().longitude);
            }
            
        });
        
        manager.startUpdatingLocation();
        CLLocationCoordinate2D location = new CLLocationCoordinate2D(latitude, longitude);
        region = new MKCoordinateRegion(location, span);
        mapView.setRegion(region);
        root.addSubview(mapView);
        
        UIButton button = UIButton.buttonWithType(UIButtonType.RoundedRect);
        button.setFrame(new CGRect(40, 370, 240, 30));
        button.setTitle("Get Times", UIControlState.Normal);
        /**
         * When the user selects the UIButton they confirm their location, and 
         * the streetPage is pushed onto the NavController
         * stack with the current longitude and latitude. 
         */
        button.addTarget(new UIControlDelegate() {
            public void raiseEvent(UIControl uic, int i) {
                getNavigationController().pushViewController(new StreetPage(latitude, longitude), true);
            }
        }, UIControlEvent.TouchUpInside);
        root.addSubview(button);
        
        setView(root);
    }
}

